package com.axn;

import com.alibaba.excel.EasyExcelFactory;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.metadata.Sheet;
import org.apache.poi.ss.formula.functions.T;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Author : 尤毅鹤
 * Date: 2019/11/21 18:44
 * Description: <描述>
 */
public class EasyExecl {
    private static Sheet initSheet = null;

    static {
        initSheet = new Sheet(1, 0);
        initSheet.setAutoWidth(Boolean.TRUE);
        initSheet.setSheetName("sheet");
    }

    public static void main(String[] args) {
        // simpleTest();
        // exportAppleTest();
        List<Student> students = new ArrayList<>();

        for (int i=0; i<1000000;i++){
            students.add(new Student("w"+i,i,i%2==0 ? "man" : "woman","l"+i,"w"+i,i,i%2==0 ? "man" : "woman","l"+i,"w"+i,i,i%2==0 ? "man" : "woman","l"+i,"w"+i,i,i%2==0 ? "man" : "woman","l"+i,"w"+i,i,i%2==0 ? "man" : "woman","l"+i));
        }

        List<String> head = new ArrayList<>();
        Field[] declaredFields = Student.class.getDeclaredFields();
        Arrays.stream(declaredFields).forEach(f -> head.add(f.getName()));
        List<List<Object>> data = getExportDataList(students, new Student());
        String filePath = "D://test5.xlsx";
        long startTime = System.currentTimeMillis();
        writeSimpleExcel(null, head, data, filePath);
        System.out.println(System.currentTimeMillis() - startTime);
    }

    private static void exportAppleTest() {
        List<Apple> apples = Arrays.asList(new Apple("red", 2d, 4d), new Apple("green", 2d, 4d), new Apple("yello", 2d, 4d));

        List<String> head = new ArrayList<>();
        Field[] declaredFields = Apple.class.getDeclaredFields();
        Arrays.stream(declaredFields).forEach(f -> head.add(f.getName()));
        List<List<Object>> data = getExportDataList(apples, new Apple());
        String filePath = "D://test3.xlsx";
        writeSimpleExcel(null, head, data, filePath);
    }

    private static void simpleTest() {
        List<List<Object>> data = new ArrayList<>();
        data.add(Arrays.asList(1, 3, 4));
        data.add(Arrays.asList(1, 3, 4));
        data.add(Arrays.asList(1, 3, 4));
        data.add(Arrays.asList(1, 3, 4));
        List<String> head = Arrays.asList("1", "3", "4");

        String filePath = "D://test.xlsx";
        writeSimpleExcel(null, head, data, filePath);
    }

    public static <K> List<List<Object>> getExportDataList(List<K> apples, K t) {
        Field[] declaredFields = t.getClass().getDeclaredFields();
        List<List<Object>> data = new ArrayList<>();
        for (K s : apples) {
            List<Object> list = new ArrayList<>();
            for (int index = 0; index < declaredFields.length; index++) {
                declaredFields[index].setAccessible(true);
                String name = declaredFields[index].getName();
                try {
                    Object o = declaredFields[index].get(s);
                    list.add(o);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
            data.add(list);
        }
        return data;
    }

    // 生成excel

    public static void writeSimpleExcel(Sheet sheet, List<String> head, List<List<Object>> data, String filePath) {
        sheet = sheet == null ? initSheet : sheet;
        if (head != null) {
            List<List<String>> lists = new ArrayList<>();
            head.forEach(h -> lists.add(Collections.singletonList(h)));
            sheet.setHead(lists);
        }

        OutputStream outputStream = null;
        ExcelWriter writer = null;
        try {
            outputStream = new FileOutputStream(filePath);
            writer = EasyExcelFactory.getWriter(outputStream);
            writer.write1(data, sheet);
        } catch (Exception e) {

        } finally {
            try {
                if (writer != null) {
                    writer.finish();
                }
                if (outputStream != null) {
                    outputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}


class Apple implements Serializable {
    private String color;
    private Double price;
    private Double weight;

    public Apple() {
    }

    public Apple(String color, Double price, Double weight) {
        this.color = color;
        this.price = price;
        this.weight = weight;
    }

    public String getColor() {
        return color;
    }

    public void setColor(String color) {
        this.color = color;
    }

    public Double getPrice() {
        return price;
    }

    public void setPrice(Double price) {
        this.price = price;
    }

    public Double getWeight() {
        return weight;
    }

    public void setWeight(Double weight) {
        this.weight = weight;
    }

    @Override
    public String toString() {
        return "Apple{" +
                "color='" + color + '\'' +
                ", price=" + price +
                ", weight=" + weight +
                '}';
    }
}


class Student implements Serializable {
    private String name;
    private Integer age;
    private String sex;
    private String teacher;
    private String name2;
    private Integer age2;
    private String sex2;
    private String teacher2;
    private String name3;
    private Integer age3;
    private String sex3;
    private String teacher3;
    private String name4;
    private Integer age4;
    private String sex4;
    private String teacher4;
    private String name5;
    private Integer age5;
    private String sex5;
    private String teacher5;

    public Student() {
    }

    public Student(String name, Integer age, String sex, String teacher, String name2, Integer age2, String sex2, String teacher2, String name3, Integer age3, String sex3, String teacher3, String name4, Integer age4, String sex4, String teacher4, String name5, Integer age5, String sex5, String teacher5) {
        this.name = name;
        this.age = age;
        this.sex = sex;
        this.teacher = teacher;
        this.name2 = name2;
        this.age2 = age2;
        this.sex2 = sex2;
        this.teacher2 = teacher2;
        this.name3 = name3;
        this.age3 = age3;
        this.sex3 = sex3;
        this.teacher3 = teacher3;
        this.name4 = name4;
        this.age4 = age4;
        this.sex4 = sex4;
        this.teacher4 = teacher4;
        this.name5 = name5;
        this.age5 = age5;
        this.sex5 = sex5;
        this.teacher5 = teacher5;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    public String getTeacher() {
        return teacher;
    }

    public void setTeacher(String teacher) {
        this.teacher = teacher;
    }

    public String getName2() {
        return name2;
    }

    public void setName2(String name2) {
        this.name2 = name2;
    }

    public Integer getAge2() {
        return age2;
    }

    public void setAge2(Integer age2) {
        this.age2 = age2;
    }

    public String getSex2() {
        return sex2;
    }

    public void setSex2(String sex2) {
        this.sex2 = sex2;
    }

    public String getTeacher2() {
        return teacher2;
    }

    public void setTeacher2(String teacher2) {
        this.teacher2 = teacher2;
    }

    public String getName3() {
        return name3;
    }

    public void setName3(String name3) {
        this.name3 = name3;
    }

    public Integer getAge3() {
        return age3;
    }

    public void setAge3(Integer age3) {
        this.age3 = age3;
    }

    public String getSex3() {
        return sex3;
    }

    public void setSex3(String sex3) {
        this.sex3 = sex3;
    }

    public String getTeacher3() {
        return teacher3;
    }

    public void setTeacher3(String teacher3) {
        this.teacher3 = teacher3;
    }

    public String getName4() {
        return name4;
    }

    public void setName4(String name4) {
        this.name4 = name4;
    }

    public Integer getAge4() {
        return age4;
    }

    public void setAge4(Integer age4) {
        this.age4 = age4;
    }

    public String getSex4() {
        return sex4;
    }

    public void setSex4(String sex4) {
        this.sex4 = sex4;
    }

    public String getTeacher4() {
        return teacher4;
    }

    public void setTeacher4(String teacher4) {
        this.teacher4 = teacher4;
    }

    public String getName5() {
        return name5;
    }

    public void setName5(String name5) {
        this.name5 = name5;
    }

    public Integer getAge5() {
        return age5;
    }

    public void setAge5(Integer age5) {
        this.age5 = age5;
    }

    public String getSex5() {
        return sex5;
    }

    public void setSex5(String sex5) {
        this.sex5 = sex5;
    }

    public String getTeacher5() {
        return teacher5;
    }

    public void setTeacher5(String teacher5) {
        this.teacher5 = teacher5;
    }


    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                ", teacher='" + teacher + '\'' +
                ", name2='" + name2 + '\'' +
                ", age2=" + age2 +
                ", sex2='" + sex2 + '\'' +
                ", teacher2='" + teacher2 + '\'' +
                ", name3='" + name3 + '\'' +
                ", age3=" + age3 +
                ", sex3='" + sex3 + '\'' +
                ", teacher3='" + teacher3 + '\'' +
                ", name4='" + name4 + '\'' +
                ", age4=" + age4 +
                ", sex4='" + sex4 + '\'' +
                ", teacher4='" + teacher4 + '\'' +
                ", name5='" + name5 + '\'' +
                ", age5=" + age5 +
                ", sex5='" + sex5 + '\'' +
                ", teacher5='" + teacher5 + '\'' +
                '}';
    }
}



